package com.sky.earnmoney;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;

import java.util.HashMap;
import java.util.List;

public class NetWork {


    interface Api{
        //获取列表
        @FormUrlEncoded
        @POST("/api/index/getGoodsList")
        GoodsListInfo getGoodsList(@FieldMap HashMap<String,Object> param);

        //获取详情信息
        @POST("/api/index/getGoodsInfo")
        GoodsDetail getGoodsInfo(@QueryMap HashMap<String,Object> param);

        //发起抢单  ** 改为对应的地址，GetParam里面构造对应的请求参数
        @POST("**")
        Object request(@QueryMap HashMap<String,Object> param);
    }

    public static void main(String[] args) {

        String token = "307m22757365726964223l353938342n22746q6m656p223l223661623163666165663966303866396466363066393961373737343062646635227o11";
        Retrofit retrofit =  new Retrofit.Builder().baseUrl("http://wxyh007.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        com.sky.earnmoney.api.Api api = retrofit.create(com.sky.earnmoney.api.Api.class);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("page",1);
        hashMap.put("limit",1);
        hashMap.put("category_id",1);
        hashMap.put("sort","all");
        hashMap.put("token",token);
        api.getGoodsList(hashMap).enqueue(new Callback<GoodsListInfo>() {
            @Override
            public void onResponse(Call<GoodsListInfo> call, Response<GoodsListInfo> response) {
                GoodsListInfo goodsListInfo = response.body();
                if(goodsListInfo != null){
                    //商品列表
                    List<GoodsListInfo.DataBean> goodsList = goodsListInfo.getData();
                    if(goodsList != null && goodsList.size() > 0){
                        //获取最小的bean
                        GoodsListInfo.DataBean minBean = goodsList.get(0);
                        double minPrice = minBean.getPrice();
                        for (int i = 1; i < goodsList.size(); i++) {
                            if(minPrice > goodsList.get(i).getPrice()){
                                minPrice = goodsList.get(i).getPrice();
                                minBean = goodsList.get(i);
                            }
                        }

                        //然后获取详情信息
                        HashMap<String, Object> infoMap = new HashMap<>();
                        infoMap.put("id",minBean.getId());
                        infoMap.put("token",token);
                        api.getGoodsInfo(infoMap).enqueue(new Callback<GoodsDetail>() {
                            @Override
                            public void onResponse(Call<GoodsDetail> call, Response<GoodsDetail> response) {
                                GoodsDetail goodsInfo = response.body();
                                //发起抢单
                                HashMap<String, Object> requestMap = new HashMap<>();
                                api.request(requestMap);
                            }

                            @Override
                            public void onFailure(Call<GoodsDetail> call, Throwable t) {

                            }
                        });



                    }
                }
            }

            @Override
            public void onFailure(Call<GoodsListInfo> call, Throwable t) {

            }
        });


    }
}
